package com.atguigu.realtime.logger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/24 9:59
 */
@Controller
@ResponseBody
@Slf4j
public class LoggerController {
    
    @RequestMapping("/applog")
    public String logger(@RequestParam("param") String log){
        // 1. 把日志落盘
        saveToDisk(log);
        
        // 2. 把日志数据直接写入到Kafka中
        writeToKafka(log);
        
        return "ok";
    }
    
    @Autowired
    KafkaTemplate<String, String> kafka;
    private void writeToKafka(String log) {
        kafka.send("ods_log", log);
    }
    
    private void saveToDisk(String data) {
        log.info(data);
    }
    
}
